package cn.gzsxt.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.gzsxt.po.PageCheckResult_name;
import cn.gzsxt.po.T_sysUser;
import cn.gzsxt.po.T_task;
import cn.gzsxt.po.T_trans;
import cn.gzsxt.query.PageQueryObject;
import cn.gzsxt.service.TaskChangeService;
import cn.gzsxt.service.impl.TaskChangeServiceImpl;
import cn.gzsxt.vo.MsgObj;

@WebServlet("/taskChangePage.sxt")
public class TaskChangeController extends HttpServlet {

	/**
	 * 
	 */

	private TaskChangeService service = new TaskChangeServiceImpl();

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String cmd = req.getParameter("cmd");
		if ("checkTrans".equals(cmd)) {
			checkTrans(req, resp);
		} else if ("deltask".equals(cmd)) {
			delTrans(req, resp);
		} else if ("updateTask".equals(cmd)) {
			updateTrans(req, resp);
		} else if ("checkId_Name".equals(cmd)) {
			checkId_Name(req, resp);
		} else if ("checkById".equals(cmd)) {
			checkById(req, resp);
		} else if ("delpagetask".equals(cmd)) {
			delPageTrans(req, resp);
		}

	}

	private void delPageTrans(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		String id = req.getParameter("id");
		String[] ids = id.split(",");
		MsgObj msgObj = new MsgObj(0, "删除失败");
		if (id != null && !"".equals(id.trim())) {
			Integer state = service.delpagetrans(ids);
			if (state == ids.length) {
				msgObj = new MsgObj(1, "删除成功");
			}
		}
		resp.getWriter().print(JSON.toJSONString(msgObj));

	}

	private void checkById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		String id = req.getParameter("id");
		System.out.println(id);
		List<T_trans> list = service.checkById(id);
		System.out.println(list);
		resp.getWriter().print(JSON.toJSONString(list));
	}

	private void checkId_Name(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		List<T_sysUser> list = service.checkId_Name();
		resp.getWriter().print(JSON.toJSONString(list));
	}

	private void updateTrans(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		// 插入和修改一起
		T_trans trans = getParameter2Obj(req);
		int row = 0;
		if (trans.getId() == null) {
			row = service.updateTrans(trans);
		} else {
			Integer id = trans.getId();
			if (trans.getDelaystatus() == 1) {
				row = service.updateTrans(trans, id, 2);
				String taskid = req.getParameter("taskid");
				if (taskid != null && !"".equals(taskid.trim())) {
					T_task task = new T_task();
					task.setFintime(trans.getTransfintime());
					task.setContent(trans.getContent());
					task.setPerson(Integer.valueOf(trans.getAcceptor()));
					row = service.updateTask(task, trans.getTaskid());
				}
			} else if (trans.getDelaystatus() == 2) {
				row = service.updateTrans(trans, id, 3);
			}

		}

		MsgObj msgObj = null;
		if (row == 1) {// 修改成功
			msgObj = new MsgObj(1, "修改成功");
		} else {
			msgObj = new MsgObj(0, "修改失败");
		}
		resp.getWriter().print(JSON.toJSONString(msgObj));
	}

	private void delTrans(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		String id = req.getParameter("id");
		MsgObj msgObj = new MsgObj(0, "删除失败");
		if (id != null && !"".equals(id.trim())) {
			Integer state = service.delTrans(id);
			if (state == 1) {
				msgObj = new MsgObj(1, "删除成功");
			}
		}
		resp.getWriter().print(JSON.toJSONString(msgObj));
	}

	private void checkTrans(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		String keyword = req.getParameter("keyword");
		PageQueryObject qo = new PageQueryObject();
		if (currentPage != null) {
			qo.setCurrentPage(Integer.valueOf(currentPage));
		}
		if (pageSize != null) {
			qo.setPageSize(Integer.valueOf(pageSize));
		}
		if (keyword != null && !"".equals(keyword.trim())) {

			qo.setKeyword(keyword);
		}
		PageCheckResult_name pageResult = service.trans(qo);
		PrintWriter out = resp.getWriter();
		String js = JSON.toJSONString(pageResult);
		System.out.println(js);
		out.print(JSON.toJSONString(pageResult));
		out.flush();// 不加就json封两次就过去了.可见输出时刷新多重要.
	}

	public T_trans getParameter2Obj(HttpServletRequest req) {
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		String delayclerk = req.getParameter("delayclerk");
		String applytime = req.getParameter("applytime");
		String transreason = req.getParameter("transreason");
		String delaystatus = req.getParameter("delaystatus");
		String replyman = req.getParameter("replyman");
		String replytime = req.getParameter("replytime");
		String transoption = req.getParameter("transoption");
		String transstatus = req.getParameter("transstatus");
		String taskid = req.getParameter("taskid");
		String acceptor = req.getParameter("acceptor");
		String transfintime = req.getParameter("transfintime");
		String id = req.getParameter("id");

		T_trans trans = new T_trans();
		trans.setTitle(title);
		trans.setContent(content);
		trans.setDelayclerk(delayclerk);
		trans.setTransreason(transreason);
		trans.setReplyman(replyman);
		trans.setTransoption(transoption);
		trans.setAcceptor(acceptor);

		if (delaystatus != null && !"".equals(delaystatus.trim())) {
			trans.setDelaystatus(Integer.valueOf(delaystatus));
		}
		if (transstatus != null && !"".equals(transstatus.trim())) {
			trans.setTransstatus(Integer.valueOf(transstatus));
		}
		if (taskid != null && !"".equals(taskid.trim())) {
			trans.setTaskid(Integer.valueOf(taskid));
		}
		if (id != null && !"".equals(id.trim())) {
			trans.setId(Integer.valueOf(id));
		}
		try { // 格式化日期
			Date data = null;
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

			if (applytime != null && !"".equals(applytime.trim())) {
				data = format.parse(applytime);
				trans.setApplytime(data);
			}
			if (replytime != null && !"".equals(replytime.trim())) {
				data = format.parse(replytime);
				trans.setReplytime(data);
			}
			if (transfintime != null && !"".equals(transfintime.trim())) {
				data = format.parse(transfintime);
				trans.setTransfintime(data);
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return trans;
	}
}
